﻿using System.Web.Mvc;
using System.Web.Script.Serialization;
using MvcApp.Models;

namespace MvcApp.Controllers
{
	public class TestOneController : Controller
	{
		//
		// GET: /TestOne/

		public ActionResult Index()
		{
			var list = DataSourceOne.GetCalculations();
			return View(list);
		}

		//
		// GET: /TestOne/Details/5

		public ActionResult Details(int id)
		{
			return View();
		}

		//
		// GET: /TestOne/Create

		public ActionResult Create()
		{
			return View();
		}

		//
		// POST: /TestOne/Create

		[HttpPost]
		public ActionResult Create(FormCollection collection)
		{
			try
			{
				// TODO: Add insert logic here

				return RedirectToAction("Index");
			}
			catch
			{
				return View();
			}
		}

		public ActionResult EditNet()
		{
			var calcs = DataSourceOne.GetCalculations();

			var datatypes = new SelectList(DataSourceOne.GetDatatypes(), "Id", "Name");

			var jsSerializer = new JavaScriptSerializer();
			ViewBag.CalcData = jsSerializer.Serialize(calcs);
			ViewBag.Datatypes = jsSerializer.Serialize(datatypes);
			
			return View(calcs);
		}

		//
		// GET: /TestOne/Edit/5

		public ActionResult Edit()
		{
			var calcs = DataSourceOne.GetCalculations();

			var datatypes = new SelectList(DataSourceOne.GetDatatypes(), "Id", "Name");

			/*var serializer = JsonSerializer.Create(new JsonSerializerSettings() { });
			var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings() { });
			var sb = new StringBuilder();
			var jsonStream = new JsonTextWriter(new StringWriter(sb)) { QuoteName = false };
			jsonSerializer.Serialize(jsonStream, calcs);*/

			var jsSerializer = new JavaScriptSerializer();
			ViewBag.CalcData = jsSerializer.Serialize(calcs);
			//ViewBag.CalcData = sb.ToString();

			ViewBag.Datatypes = jsSerializer.Serialize(datatypes);

			return View(calcs);
		}

		//
		// POST: /TestOne/Edit/5

		[HttpPost]
		public ActionResult Edit(int id, FormCollection collection)
		{
			try
			{
				// TODO: Add update logic here

				return RedirectToAction("Index");
			}
			catch
			{
				return View();
			}
		}

		//
		// GET: /TestOne/Delete/5

		public ActionResult Delete(int id)
		{
			return View();
		}

		//
		// POST: /TestOne/Delete/5

		[HttpPost]
		public ActionResult Delete(int id, FormCollection collection)
		{
			try
			{
				// TODO: Add delete logic here

				return RedirectToAction("Index");
			}
			catch
			{
				return View();
			}
		}
	}
}
